$(function () {
    $('.sidebar-menu').find('li').click(function () {
        var li = $(this);
        if (li.hasClass("active")) {
            return;
        }
        li.siblings("li").removeClass("active");
        li.addClass('active');
        var url = li.data("url");
        gm.loadPage(url);
    });
});

function delSku(id) {
    $.get(baseUri + '/goods/skuDelete?id=' + id,function (ret) {
            console.log(ret.message);
            table.ajax.reload();
    });
}

function delGoods(id) {
    $.get(baseUri + '/goods/goodsDelete?id=' + id,
        function (ret) {
            table.ajax.reload();
        });
}

function saveGoods() {
    var data = $('#goodsAddForm').serialize();
    $.post(baseUri + '/goods/typeAdd', data, function (ret) {
        table.ajax.reload();
    });
}

function editGoods(id) {
    var row = table.row(id).data();
    $('#goodsId').val(row.id);
    $('#goodsName').val(row.name);
    $('#serialNo').val(row.serialNo);
    $('#storeCnt').val(row.cnt);
    $('#goodsPosition').val(row.position);
}

function editSku(id) {
    var row = table.row(id).data();
    $('#goodsId').val(row.id);
    $('#goodsName').val(row.goods.name);
    $('#goodsPosition').val(row.position);
    $('#serialNo1').val(row.serialNo);
}

var gm = (function (mod) {
    mod.saveTag = function () {
        var data = $('#tagAddForm').serialize();
        // noinspection JSUnresolvedFunction
        $.post(baseUri + '/home/tagAdd', data, function (ret) {
            if (ret.code === 200) {
                gm.loadPage('/goods');
                gm.reCategory();
            } else {
                layer.msg(ret.message);
            }
        })
    };
    mod.loadPage = function loadPage(url) {
        $.get(url, function (data) {
            $('#mainContent').html(data);
            $('.modal-backdrop.fade.in').remove();
            initPage();
        })
    };

    mod.delTag = function (id) {
        $.get(baseUri + '/api/delTag?id=' + id , function (ret) {
            layer.msg(ret.data, {
                time: 1000
            }, function () {
                table.ajax.reload();
            });
        })
    };

    mod.reCategory = function reCategory() {
        $.get(baseUri + '/home/recategory', function (ret) {
            layer.msg(ret.data, {
                time: 1000
            }, function () {
                table.ajax.reload();
            });
        });
    };

    mod.clearForm = function () {
        $('#goodsAddForm')[0].reset();
        $('#tagAddForm')[0].reset();
    };
    return mod;
})(window.gm || {});